package com.jrx.anytxn.param.controller.system;

import com.google.common.collect.Maps;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.param.dto.system.TransactionCodeTableReq;
import com.jrx.anytxn.param.dto.system.TransactionCodeTableRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.system.ITransactionCodeTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;

/**
 * 交易码表
 *
 * @author Yangxin
 * @date 2019/8/2
 */
@Api(tags = "交易码参数api接口")
@RestController
@RequestMapping(value = "/param/transactionCodes")
public class TransactionCodeTableController {
    private static final Logger logger = LoggerFactory.getLogger(TransactionCodeTableController.class);
    @Autowired
    ITransactionCodeTableService iTransactionCodeTableService;
    @Autowired
    private InfoService infoService;

    @ApiOperation(value = "通过交易码和租户ID查询交易码表信息", notes = "通过交易码和租户ID查询交易码表信息")
    @GetMapping(value = "/{transactionCode}/{tenantId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "transactionCode", value = "交易码", required = true),
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true)
    })
    public TxnRespResult<TransactionCodeTableRes> getById(@NotNull @PathVariable String transactionCode, @PathVariable("tenantId") String tenantId) {
        TxnRespResult<TransactionCodeTableRes> result = new TxnRespResult<>();
        TransactionCodeTableRes res;
        try {
            res = iTransactionCodeTableService.findByCodeAndTenId(transactionCode, String.valueOf(infoService.getUser().getProjectId()));
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID分页查询交易码表信息", notes = "根据租户ID分页查询交易码表信息")
    @GetMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<TransactionCodeTableRes>> getAllPageByTenId(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize,@RequestParam("transactionCode") String transactionCode) {

        TxnRespResult<TxnPage<TransactionCodeTableRes>> result = new TxnRespResult<>();
        TxnPage<TransactionCodeTableRes> response;
        try {
            String tenantId = String.valueOf(infoService.getUser().getProjectId());
            response = iTransactionCodeTableService.findAllPageByTenId(pageNum, pageSize, tenantId,transactionCode);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }

        return result;
    }

    @ApiOperation(value = "新增交易码表信息", notes = "新增交易码表信息")
    @PostMapping("/")
    public TxnRespResult<TransactionCodeTableRes> createTransactionCodeTable(@RequestBody TransactionCodeTableReq transactionCodeTableReq) {
        TransactionCodeTableRes res;
        TxnRespResult<TransactionCodeTableRes> result = new TxnRespResult<>();
        try {
            transactionCodeTableReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            transactionCodeTableReq.setUpdateBy(infoService.getUser().getUserName());
            res = iTransactionCodeTableService.addTransactionCodeTable(transactionCodeTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "编辑交易码表信息", notes = "编辑交易码表信息")
    @PutMapping("/")
    public TxnRespResult<TransactionCodeTableRes> modifyTransactionCodeTable(@RequestBody TransactionCodeTableReq transactionCodeTableReq) {
        TxnRespResult<TransactionCodeTableRes> result = new TxnRespResult<>();
        TransactionCodeTableRes res;
        try {
            transactionCodeTableReq.setUpdateBy(infoService.getUser().getUserName());
            res = iTransactionCodeTableService.modifyTransactionCodeTable(transactionCodeTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }

        return result;
    }

    @ApiOperation(value = "根据交易码和租户ID删除交易码表信息", notes = "根据交易码和租户ID删除交易码表信息")
    @DeleteMapping(value = "/{transactionCode}/{tenantId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "transactionCode", value = "主键", required = true),
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true)
    })
    public TxnRespResult<Boolean> removeById(@NotNull @PathVariable String transactionCode, @PathVariable("tenantId") String tenantId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);

            flag = iTransactionCodeTableService.removeByCodeAndTenId(transactionCode, String.valueOf(infoService.getUser().getProjectId()));
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID获得交易码表信息", notes = "根据租户ID获得交易码表信息")
    @GetMapping(value = "/list")
    public TxnRespResult<HashMap<String, List<TransactionCodeTableRes>>> getAllPage() {
        TxnRespResult<HashMap<String, List<TransactionCodeTableRes>>> result = new TxnRespResult<>();

        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        List<TransactionCodeTableRes> allOrg = iTransactionCodeTableService.findByTenId(tenantId);

        HashMap<String, List<TransactionCodeTableRes>> sysCodeMap = Maps.newHashMap();
        sysCodeMap.put("listData", allOrg);
        return result.getSuccess(sysCodeMap);
    }

}
